package com.example.ChanOfResponsibility2;

/**
 * 责任链 - 设计模式
 *  https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html
 *  一个 logger 有2层定义: level 和 输出位置
 */
public class AppTest {
    /**
     * 获取log链
     *  logger:
     *      errorLog → fileLog → consoleLog
     *  level:
     *      ERROR 3  → DEBUG 2 → INFO 1
     */
    public static AbstractLogger getLog() {
        AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);     // 3
        AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);       // 2
        AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);  // 1

        errorLogger.setNextLogger(fileLogger);
        fileLogger.setNextLogger(consoleLogger);

        return errorLogger;
    }

    public static void main(String[] args) {
        AbstractLogger log = getLog();

        log.logMessage(AbstractLogger.INFO, "info log");    // 3<=, 2<=, 1<=    1
        log.logMessage(AbstractLogger.DEBUG, "debug log");  // 3<=, 2<=, 1<=    2
        log.logMessage(AbstractLogger.ERROR, "error log");  // 3<=, 2<=, 1<=    3

        // Output:
        //     Console log:	    info log
        //     File log:		debug log
        //     Console log:	    debug log
        //     Error log:		error log
        //     File log:		error log
        //     Console log:	    error log
    }
}
